﻿namespace com.stifel.Extensions.SharePointExtensions
{
    using System;

    using Microsoft.SharePoint;

    public static class SPListCollectionExtensions
    {
        #region Methods

        /// <summary>
        /// Indicates whether the list exists as specified by the named folder, with the collection of SharePoint list objects. The named folder contains all the files that are used in working with the list.
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="rootFolderName"></param>
        /// <returns>Return true if the list exists within the collection; otherwise false.</returns>
        public static bool ContainsList( this SPListCollection lists, string rootFolderName )
        {
            foreach ( SPList list in lists ) if ( list.RootFolder.Name.Equals( rootFolderName, StringComparison.OrdinalIgnoreCase ) )
                    return true;
            return false;
        }

        /// <summary>
        /// Get the SharePoint list as specified by the named folder. The named folder contains all the files that are used in working with the list.
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="rootFolderName"></param>
        /// <returns>Returns the SharePoint list as specified by the named folder.</returns>
        public static SPList GetListByRootFolder( this SPListCollection lists, string rootFolderName )
        {
            foreach ( SPList list in lists ) if ( list.RootFolder.Name.Equals( rootFolderName, StringComparison.OrdinalIgnoreCase ) )
                    return list;
            return null;
        }

        /// <summary>
        /// Get the GUID that identifies the list within the database as specified by the named folder. The named folder contains all the files that are used in working with the list.
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="rootFolderName"></param>
        /// <returns>Returns the GUID that identifies the list with the database.</returns>
        public static Guid GetListID( this SPListCollection lists, string rootFolderName )
        {
            foreach ( SPList list in lists ) if ( list.RootFolder.Name.Equals( rootFolderName, StringComparison.OrdinalIgnoreCase ) )
                    return list.ID;
            return Guid.Empty;
        }

        /// <summary>
        /// Get the displayed title for the list as specified by the named folder. The named folder contains all the files that are used in working with the list.
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="rootFolderName"></param>
        /// <returns>Returns the displaed title for the list as a string.</returns>
        public static string GetListTitle( this SPListCollection lists, string rootFolderName )
        {
            foreach ( SPList list in lists ) if ( list.RootFolder.Name.Equals( rootFolderName, StringComparison.OrdinalIgnoreCase ) )
                    return list.Title;
            return string.Empty;
        }

        #endregion Methods
    }
}